package controller;

import dao.DrugDAOImpl;
import model.TDrug;
import model.TUser;
import utils.DispatcherUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AddDrugServlet", value = "/adddrug.do")
public class AddDrugServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String s = request.getParameter("drugid");
        if(s==null||s.equals("")){
            TDrug tDrug = new TDrug();
            HttpSession session = request.getSession();
            TUser tUser = (TUser) session.getAttribute("loginuser");
            tDrug.setUserid(tUser.getUserid());
            request.setAttribute("tDrug",tDrug);
        }else{
            DrugDAOImpl drugDAO = new DrugDAOImpl();
            TDrug tDrug = drugDAO.getDrugById(Integer.parseInt(s));
            request.setAttribute("tDrug",tDrug);
        }
        DispatcherUtils.openweb("adddrug.jsp",request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
